<?php

namespace App\Repository\Repositories;

use App\Models\StoreCombination;
use App\Models\StorePink;
use App\Models\StoreGroupProduct;
use App\Repository\RepositoryInterface\GroupInterface;


class GroupRepositories implements GroupInterface
{
     public function HeadGroupList()
    {
        $comb=new StoreCombination();
        $pink=new StorePink();
        $list=StorePink::with(['GroupProduct'])->where('k_id','=',0)->get()->toArray();
        return $list;
    }
    /**
     * @param $uid
     * @param $gid
     * @param $cid
     * @param $kid
     * @param $num
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|string
     * 拼团 加入
     */
    public function JoinGroup($uid,$gid,$cid,$kid,$num)
    {
        $now_time = date('Y-m-d H:i:s');
        $pink = new StorePink();
        $pink_res = $pink->IsJoin($uid,$gid,$cid,$kid);
        if($pink_res)
        {
            return response(['code'=>20002,'message'=>'您已是该团成员','data'=>[]]);
        }
        $cmob = new StoreCombination();
        $res=$cmob->GroupGoods($cid);
        $c_people=$res['people'];
        $counta=$pink->GroupCount($cid,$kid);
        $count= count($counta);
        $expire_time = date('Y-m-d h:i:s',strtotime('+1 day'));
        if($c_people==$count)
        {
            return response(['code'=>20001,'message'=>'该团已满','data'=>[]]);
        }
        $array = [
            'uid'=>$uid,
            'order_id'=>'12313123',//暂定
            'order_id_key'=>'123123123',//暂定
            'total_num'=>$num,
            'total_price'=>$res['price']*$num,
            'cid' => $res['id'],
            'pid' => $res['id'],
            'people' => 1,
            'price' => $res['price'],
            'add_time'=>$res['start_time'],
            'stop_time'=>$res['stop_time'],
            'k_id'=>$kid,
            'is_tpl'=>0,
            'is_refund'=>0,
            'status'=>1
        ];
        try {
            $res=$pink->AddPink($array);
            if($res)
            {
                return response(['code'=>20000,'message'=>'加入成功','data'=>[]]);
            }
        }catch (\Exception $exception)
        {
            return  $exception->getMessage();
        }
    }
      /**
     * @param $uid
     * @param $cid
     * @param $num
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     * 用户发起拼团
     */
    public function UserInitGroup($uid,$cid,$num,$time){
        $StorePink = new StorePink();
        $comb = new StoreCombination();
        $res = $StorePink->IfGroupHas($cid,$uid);
        $now = date('Y-m-d H:i:s');
        $expire_time = date('Y-m-d h:i:s',strtotime("+$time hour"));
        if(!empty($res))
        {
            return  response (['code' => 5000,'msg' => '您已经对该商品发起拼团','data' => []]);
        }
        $g_goods = $comb -> GroupGoods($cid);
        $data = [
            'uid' => $uid,
            'order_id' => '123123',//暂定
            'order_id_key' => '123123',//暂定
            'total_num' => $num,
            'total_price' => $g_goods['price']*$num,
            'cid' => $g_goods['id'],
            'pid' => $g_goods['product_id'],
            'people' => $g_goods['people'],
            'price' => $g_goods['price'],
            'add_time'=> $now,
            'stop_time'=> $expire_time,
            'k_id' => 0,
            'is_tpl' => 0,
            'is_refund' => 0,
            'status' => 1
        ];
        try {
            $res=$StorePink->UserInitGroup($data);
            if($res)
            {
                Redis::Lpush("$uid+$cid",1);
                return  response(['code'=>20000,'msg'=>'发起拼团成功','data'=>[]]);
            }
            else
            {
                throw new \Exception('拼团发起失败');
            }
        }catch (\Exception $exception)
        {
            return  response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }
     /**
     * @param $uid
     * @param $kid
     * @param $cid
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     * 用户退出拼团
     */
    public function UserExitGroup($uid,$kid,$cid)
    {
        $pink=new StorePink();
        $res=$pink->UserExitGroup($uid,$kid,$cid);
        if($res)
        {
            return  response(['code'=>20000,'msg'=>'退出成功','data'=>[]]);
        }
    }
    /**
     * @param $uid
     * @param $cid
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     * 团长取消开团
     */
    public function HeadExitGroup($uid,$cid)
    {
        $pink=new StorePink();
        $res=$pink->HeadExitGroup($uid,$cid);
        if($res)
        {
            return  response(['code'=>20000,'msg'=>'取消拼团成功','data'=>[]]);
        }
        else
        {
            return  response(['code'=>5000,'msg'=>'取消拼团失败','data'=>[]]);
        }
    }
    
    /**
     * 团长拼团商品推荐查询
     * @return \Illuminate\Http\JsonResponse
     */
    public function recommendGoods(){
        try {
            $res = StoreCombination::recommendGoods();
            return parent::success("查询成功",$res);
        }catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }
    /**
     * @param $uid
     * @param $cid
     * @param $num
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     * 用户发起拼团
     */
    public function UserInitGroup($uid,$cid,$num){
        $StorePink = new StorePink();
        $comb = new StoreCombination();
        $res = $StorePink->IfGroupHas($cid,$uid);
        $now = date('Y-m-d H:i:s');
        $expire_time = date('Y-m-d h:i:s',strtotime('+1 day'));
        if(!empty($res))
        {
            return  response (['code' => 5000,'msg' => '您已经对该商品发起拼团','data' => []]);
        }
        $g_goods = $comb -> GroupGoods($cid);
        $data = [
            'uid' => $uid,
            'order_id' => '123123',//暂定
            'order_id_key' => '123123',//暂定
            'total_num' => $g_goods['num'],
            'total_price' => $g_goods['price']*$num,
            'cid' => $g_goods['id'],
            'pid' => $g_goods['product_id'],
            'people' => 1,
            'price' => $g_goods['price'],
            'add_time'=> $now,
            'stop_time'=> $expire_time,
            'k_id' => 0,
            'is_tpl' => 0,
            'is_refund' => 0,
            'status' => 1
        ];
        try {
            $res=$StorePink->UserInitGroup($data);
            if($res)
            {
                return  response(['code'=>20000,'msg'=>'发起拼团成功','data'=>[]]);
            }
            else
            {
                throw new \Exception('拼团发起失败');
            }
        }catch (\Exception $exception)
        {
            return  response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }
    public function groupList($size)
    {
        try
        {
            $result = StorePink::storePinkWithUserWithStoreCombination($size);
            return response(['code'=>20000,'msg'=>'success','data'=>$result]);
        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }
    public function addGroup($post)
    {
        try
        {
            $data = [
                'uid' => $post['uid'],
                'order_id'=>time().rand(1111,9999),
                'total_num'=>$post['total_num'],
                'order_id_key'=>$post['order_id_key'],
                'total_price'=>$post['total_price'],
                'cid'=>$post['cid'],
                'pid'=>$post['pid'],
                'people'=>$post['people'],
                'price'=>$post['price'],
                'add_time'=>$post['add_time'],
                'stop_time'=>$post['stop_time'],
                'is_tpl'=>$post['is_tpl'],
                'is_refund'=>$post['is_refund'],
                'status'=>$post['status']
            ];
            StorePink::addStorePink($data);
            return response(['code'=>20000,'msg'=>'添加成功','data'=>[]]);
        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }
    public function delGroup($id)
    {
        try
        {
            $find = StorePink::findStorePink($id);

            if ($find['status'] == 1){
                return response(['code'=>5000,'msg'=>'活动正在进行中不能删除','data'=>[]]);
            }
            if ($find['status'] == 3 && $find['is_refund'] == 0){
                return response(['code'=>5000,'msg'=>'活动未完成,还没退款','data'=>[]]);
            }

            StorePink::delStorePink($id);

            return response(['code'=>20000,'msg'=>'success','data'=>[]]);

        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }
    public function groupAlter($post)
    {
        try
        {

            $find = StorePink::findStorePink($post['id']);
            if ($find['status'] == 1){
                return response(['code'=>5000,'msg'=>'活动正在进行中','data'=>[]]);
            }

            $data = [
                'total_num'=>$post['total_num'],
                'total_price'=>$post['total_price'],
                'people'=>$post['people'],
                'price'=>$post['price'],
                'add_time'=>$post['add_time'],
                'stop_time'=>$post['stop_time'],
                'is_tpl'=>$post['is_tpl'],
            ];

            StorePink::storePinkAlter($post['id'],$data);

            return response(['code'=>5000,'msg'=>'success','data'=>[]]);

        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }
    public function groupDetails($id)
    {
        try
        {
            $find = StorePink::findStorePink($id);

            return response(['code'=>20000,'msg'=>'success','data'=>$find]);

        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }

    /**
     * @param $arr
     * 拼团商品添加
     */
    public function CollageGoodsList($size)
    {
        try
        {

            $result = StoreCombination::storeCombinationWithStoreProductWithUser($size);

            return response(['code'=>20000,'msg'=>'success','data'=>$result]);

        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }

    }

    /**
     * 商品添加
     */
    public function addCollageGoods($post)
    {
        try
        {
            $data = [
                'product_id'=>$post['product_id'],
                'image'=>$post['product_id'],
                'images'=>$post['product_id'],
                'title'=>$post['product_id'],
                'attr'=>$post['product_id'],
                'people'=>$post['product_id'],
                'info'=>$post['product_id'],
                'price'=>$post['product_id'],
                'sort'=>$post['product_id'],
                'stock'=>$post['product_id'],
                'add_time'=>date('Y-m-d H:i:s'),
                'is_show'=>$post['product_id'],
                'mer_use'=>$post['product_id'],
                'is_postage'=>$post['product_id'],
                'postage'=>$post['product_id'],
                'start_time'=>strtotime($post['product_id']),
                'stop_time'=>strtotime($post['product_id']),
                'effective_time'=>strtotime($post['product_id'])-strtotime($post['product_id']),
                'cost'=>$post['product_id'],
                'temp_id'=>$post['product_id'],
                'weight'=>$post['product_id'],
                'volume'=>$post['product_id'],
                'num'=>$post['product_id'],
                'quota'=>$post['product_id'],
                'quota_show'=>$post['product_id'],
            ];

            StoreCombination::add($data);

            return response(['code'=>20000,'msg'=>'success','data'=>[]]);

        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }

    }

    public function delCollageGoods($id)
    {
        try
        {
            StoreCombination::del($id);

            return response(['code'=>20000,'msg'=>'success','data'=>[]]);

        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }

    }

    public function detailsCollageGoods($id)
    {
        try
        {
            $find = StoreCombination::find($id);

            return response(['code'=>20000,'msg'=>'success','data'=>$find]);

        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }

    }

    public function updateCollageGoods($post)
    {
        try
        {
            $result = [
                'product_id'=>$post['product_id'],
                'image'=>$post['product_id'],
                'images'=>$post['product_id'],
                'title'=>$post['product_id'],
                'attr'=>$post['product_id'],
                'people'=>$post['product_id'],
                'info'=>$post['product_id'],
                'price'=>$post['product_id'],
                'sort'=>$post['product_id'],
                'stock'=>$post['product_id'],
                'add_time'=>date('Y-m-d H:i:s'),
                'is_show'=>$post['product_id'],
                'mer_use'=>$post['product_id'],
                'is_postage'=>$post['product_id'],
                'postage'=>$post['product_id'],
                'start_time'=>strtotime($post['product_id']),
                'stop_time'=>strtotime($post['product_id']),
                'effective_time'=>strtotime($post['product_id'])-strtotime($post['product_id']),
                'cost'=>$post['product_id'],
                'temp_id'=>$post['product_id'],
                'weight'=>$post['product_id'],
                'volume'=>$post['product_id'],
                'num'=>$post['product_id'],
                'quota'=>$post['product_id'],
                'quota_show'=>$post['product_id'],
            ];

            StoreCombination::alter($post['id'],$result);

            return response(['code'=>20000,'msg'=>'success','data'=>[]]);

        }catch (\Exception $exception){
            return response(['code'=>5000,'msg'=>$exception->getMessage(),'data'=>[]]);
        }

    }


}
